package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService{

    @Autowired
    private CheckItemDao checkItemDao;

    //添加新的检查项
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    //分页查询
    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        //处理参数
        /*String queryString = queryPageBean.getQueryString();
        String _queryString = null;
        if (queryString != null){
            _queryString = "%" + queryString + "%";
        }*/
        //使用分页工具
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckItem> page = checkItemDao.queryByCondition(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }

    //根据id删除检查项
    @Override
    public void deleteById(Integer id) {
        //根据id 查询检查项的关联项个数
        Long count = checkItemDao.findLinkedDataById(id);
        if (count > 0){//有数据关联
            throw new RuntimeException();
        }
        //没有数据关联
        checkItemDao.deleteById(id);
    }

    //根据id查询检查项
    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    //根据id修改检查项
    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }

    //查询所有的检查项
    @Override
    public List<CheckItem> findAll() {

        return checkItemDao.findAll();
    }

}
